package com.kalvan.admin.system.controller;


import com.kalvan.admin.system.service.AuthService;
import com.kalvan.admin.system.service.MenuService;
import com.kalvan.commons.constant.LogGroup;
import com.kalvan.commons.db.system.model.Menu;
import com.kalvan.commons.dto.system.MenuImport;
import com.kalvan.core.exception.BizException;
import com.kalvan.core.utils.BeanUtils;
import com.kalvan.core.valid.Add;
import com.kalvan.core.valid.Edit;
import com.kalvan.web.controller.BaseController;
import com.kalvan.web.exception.AdminReturnCode;
import com.kalvan.web.i18n.annotation.I18n;
import com.kalvan.web.log.annotation.SysLog;
import com.kalvan.web.servlet.annotation.Permission;
import com.kalvan.web.servlet.response.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单资源Controller
 *
 * @author kalvan
 * @date 2019-06-01 02:29:27
 */
@I18n("i18n/com/kalvan/admin/system/menu")
@SysLog(group = LogGroup.MENU)
@Api(tags = {"菜单资源"})
@ApiIgnore
@RestController
@RequestMapping("system/menu")
@Slf4j
public class MenuController extends BaseController {
    @Resource
    private MenuService menuService;
    @Resource
    private AuthService authService;

    /**
     * 查询所有
     *
     * @return R
     */
    @ApiOperation("获取菜单资源列表")
    @PostMapping("list")
    @Permission("admin:system:menu:list")
    public R list(String systemCode) {
        List<Menu> data = authService.selectMenuList();
        return R.ok().putData(data);
    }

    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return R
     */
    @ApiOperation("主键查询")
    @GetMapping("info/{id}")
    @Permission("admin:system:menu:info")
    public R info(@PathVariable("id") Long id) {
        Menu menu = menuService.findById(id);
        if (menu == null) {
            throw new BizException(AdminReturnCode.REQUEST_ILLEGAL);
        }
        return R.ok().putData(menu);
    }

    /**
     * 新增菜单资源
     *
     * @param menuImport menuImport
     * @return R
     */
    @SysLog(remark = "新增菜单资源")
    @ApiOperation("新增菜单资源")
    @PostMapping("add")
    @Permission("admin:system:menu:add")
    public R add(@Validated({Add.class}) MenuImport menuImport) {
        menuService.add(BeanUtils.newAndCopy(menuImport, Menu.class));
        return R.ok();
    }

    /**
     * 修改菜单资源
     *
     * @param menuImport menuImport
     * @return R
     */
    @SysLog(remark = "修改菜单资源")
    @ApiOperation("修改菜单资源")
    @PostMapping("edit")
    @Permission("admin:system:menu:edit")
    public R edit(@Validated({Edit.class}) MenuImport menuImport) {
        if (menuImport.getId() == null) {
            return R.error(AdminReturnCode.REQUEST_ILLEGAL.getMsg());
        }
        menuService.edit(BeanUtils.newAndCopy(menuImport, Menu.class));
        return R.ok();
    }

    /**
     * 根据主键删除菜单资源
     *
     * @param ids ids
     * @return R
     */
    @SysLog(remark = "删除菜单资源")
    @ApiOperation("删除菜单资源")
    @PostMapping("delete")
    @Permission("admin:system:menu:delete")
    public R delete(Long[] ids) {
        if (ArrayUtils.isEmpty(ids)) {
            return R.error(AdminReturnCode.REQUEST_ILLEGAL.getMsg());
        }
        int num = menuService.deleteBatch(ids);
        return R.ok(String.format("成功删除%s行数据", num));
    }

    @ApiOperation("编辑菜单资源排序")
    @PostMapping("priority")
    @Permission("admin:system:menu:edit")
    public R editOrder(Long id, Integer priority) {
        if (id == null || priority == null) {
            return R.error(AdminReturnCode.REQUEST_ILLEGAL.getMsg());
        }
        Menu menu = new Menu();
        menu.setPriority(priority);
        menu.setId(id);
        menuService.edit(menu);
        return R.ok();
    }

}
